package io.geekidea.boot.shop.controller.app;

import io.geekidea.boot.auth.annotation.Permission;
import io.geekidea.boot.common.enums.SysLogType;
import io.geekidea.boot.framework.annotation.Log;
import io.geekidea.boot.framework.page.Paging;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.shop.dto.AddressDto;
import io.geekidea.boot.shop.query.web.AddressQuery;
import io.geekidea.boot.shop.service.base.api.AddressService;
import io.geekidea.boot.shop.vo.web.AddressVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 收货地址 控制器
 *
 * @author geekidea
 * @since 2024-10-27
 */
@Slf4j
@RestController
@Tag(name = "收货地址")
@RequestMapping("/admin/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    /**
     * 添加收货地址
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.ADD)
    @Operation(summary = "添加收货地址")
    @PostMapping("/addAddress")
    @Permission("address:add")
    public ApiResult addAddress(@Valid @RequestBody AddressDto dto) {
        log.info("添加收货地址：{}", dto);
        boolean flag = addressService.addAddress(dto);
        return ApiResult.result(flag);
    }

    /**
     * 修改收货地址
     *
     * @param dto
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.UPDATE)
    @Operation(summary = "修改收货地址")
    @PostMapping("/updateAddress")
    @Permission("address:update")
    public ApiResult updateAddress(@Valid @RequestBody AddressDto dto) {
        log.info("修改收货地址：{}", dto);
        boolean flag = addressService.updateAddress(dto);
        return ApiResult.result(flag);
    }

    /**
     * 删除收货地址
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(type = SysLogType.DELETE)
    @Operation(summary = "删除收货地址")
    @PostMapping("/deleteAddress/{id}")
    @Permission("address:delete")
    public ApiResult deleteAddress(@PathVariable Integer id) {
        log.info("删除收货地址：{}", id);
        boolean flag = addressService.deleteAddress(id);
        return ApiResult.result(flag);
    }

    /**
     * 获取收货地址详情
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取收货地址详情")
    @PostMapping("/getAddress/{id}")
    @Permission("address:info")
    public ApiResult<AddressVo> getAddress(@PathVariable Integer id) {
        log.info("获取收货地址详情：{}", id);
        AddressVo addressVo = addressService.getAddressById(id);
        return ApiResult.success(addressVo);
    }

    /**
     * 获取收货地址分页列表
     *
     * @param query
     * @return
     * @throws Exception
     */
    @Operation(summary = "获取收货地址分页列表")
    @PostMapping("/getAddressPage")
    @Permission("address:page")
    public ApiResult<AddressVo> getAddressPage(@Valid @RequestBody AddressQuery query) {
        log.info("获取收货地址分页列表：{}", query);
        Paging<AddressVo> paging = addressService.getAddressPage(query);
        return ApiResult.success(paging);
    }

}
